//
//  NSDictionary+SLILibs.m
//  HeiHei
//
//  Created by LiJie on 14-10-3.
//  Copyright (c) 2014年 com.slient. All rights reserved.
//

#import "SLILibs+NSDictionary.h"

@implementation NSDictionary (SLILibs)

/// 获取字符串，默认值为@""
- (NSString *)stringForKey:(id)key
{
    if (key)
    {
        NSString *value = [self objectForKey:key];
        if (value && [value isKindOfClass:[NSString class]])
        {
            return value;
        }
    }
    return @"";
}

/// 获取NSNumber，默认值为@0
- (NSNumber *)numberForKey:(id)key
{
    return [self numberForKey:key default:@0];
}

/// 获取NSNumber，默认值为defValue
- (NSNumber *)numberForKey:(id)key default:(NSNumber *)defValue
{
    if (key)
    {
        NSNumber *value = [self objectForKey:key];
        if (value && [value isKindOfClass:[NSNumber class]])
        {
            return value;
        }
    }
    return (defValue ? defValue : @0);
}

/// 获取整型，默认值为0
- (int)intForKey:(id)key
{
    return [self intForKey:key default:0];
}

/// 获取整型，默认值为defValue
- (int)intForKey:(id)key default:(int)defValue
{
    int num = defValue;
    id obj = [self objectForKey:key];
    if ([obj isKindOfClass:[NSString class]] ||
        [obj isKindOfClass:[NSNumber class]])
    {
        num = [obj intValue];
    }
    return num;
}

/// 获取NSArray，默认值为nil
- (NSArray *)arrayForKey:(id)key
{
    if (key)
    {
        NSArray *value = [self objectForKey:key];
        if (value && [value isKindOfClass:[NSArray class]])
        {
            return value;
        }
    }
    return nil;
}

/// 获取NSDictionary，默认值为nil
- (NSDictionary *)dictionaryForKey:(id)key
{
    if (key)
    {
        NSDictionary *value = [self objectForKey:key];
        if (value && [value isKindOfClass:[NSDictionary class]])
        {
            return value;
        }
    }
    return nil;
}

@end
